/**
 * AspectSense project
 *
 * Middleware for annotation-based, context-aware and adaptive services for
 * mobile and ubiquitous computing
 *
 * This project is an open-source effort, started at the University of Cyprus
 * [http://www.ucy.ac.cy].
 *
 * More information about the project is available at
 * [http://code.google.com/p/aspectsense]
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package org.aspectsense.example_apps.multipleUI;

import org.aspectsense.annotations.Component;
import org.aspectsense.annotations.Service;
import org.aspectsense.annotations.ServiceUtility;
import org.aspectsense.framework.DefaultComponent;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Logger;

/**
 * <p/>
 * Date: Jul 13, 2007<br/>
 * Time: 2:41:42 PM
 * </p>
 *
 * @author Nearchos Paspallis [nearchos@cs.ucy.ac.cy]
 */
@Component(
        displayID = "VisualUI"
)
@Service(
        serviceType = UIService.class
)
@ServiceUtility(
        targetServiceType = UIService.class,
        utilityFunction = "context[context.user.handsfree-required] ? 0 : 1"
)
public class VisualUI extends DefaultComponent implements UIService
{
    private JFrame frame;

    private JLabel label;

    // ----------------- Handling life-cycle operations --------------------- //

    public void init()
    {
        frame = new JFrame("Visual UI Component") {
            public Dimension getPreferredSize()
            {
                return new Dimension(400,100);
            }
        };
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        frame.getContentPane().setLayout(new BorderLayout());

        label = new JLabel("", JLabel.CENTER);
        frame.getContentPane().add(label, BorderLayout.CENTER);

        frame.setLocation(100,100);
        frame.pack();
    }

    public void activate()
    {
        frame.setVisible(true);
    }

    public void suspend()
    {
        frame.setVisible(false);
    }

    public void finit()
    {
        frame.dispose();
        frame = null;
        label = null;
    }

    // -------------- End of Handling life-cycle operations ----------------- //

    public void communicateText(final String text)
    {
        Logger.getAnonymousLogger().finest("Communicating text: " + text);

        label.setText(text);
    }
}